#!/bin/sh /etc/rc.common

START=96
STOP=10

extra_command "add_rule"
extra_command "del_rule"
extra_command "reload_rule"

PROG_PATH=/usr/share/adbyby
DATA_PATH=$PROG_PATH/data
WAN_FILE=/tmp/dnsmasq.d/dnsmasq-adbyby.d/03-adbyby-ipset.conf
CRON_FILE=/etc/crontabs/root
CONFIG=adbyby
ipt_n="iptables -t nat"

uci_get_by_name() {
	local ret=$(uci get $CONFIG.$1.$2 2>/dev/null)
	echo ${ret:=$3}
}

uci_get_by_type() {
	local index=0
	if [ -n $4 ]; then
		index=$4
	fi
	local ret=$(uci get $CONFIG.@$1[$index].$2 2>/dev/null)
	echo ${ret:=$3}
}

get_config()
{
	config_get_bool enable $1 enable 0
	config_get_bool cron_mode $1 cron_mode 1
	config_get wan_mode $1 wan_mode 1
	config_get_bool block_ios $1 block_ios 0
	config_get_bool mem_mode $1 mem_mode 1
	config_get_bool block_cnshort $1 block_cnshort 0
}

add_rules()
{
	rm -f $DATA_PATH/user.bin
	grep -v ^! $PROG_PATH/rules.txt > $DATA_PATH/user.txt
	cp $PROG_PATH/rules.txt $DATA_PATH/rules.txt
}

add_cron()
{
	if [ $cron_mode -eq 1 ]; then
		sed -i '/adblock.sh/d' $CRON_FILE
		echo '0 6 * * * /usr/share/adbyby/adblock.sh > /tmp/adupdate.log 2>&1' >> $CRON_FILE
		crontab $CRON_FILE
	fi
}

del_cron()
{
	sed -i '/adblock.sh/d' $CRON_FILE
	/etc/init.d/cron restart
}

ip_rule()
{
	ipset -N adbyby_esc hash:ip
	$ipt_n -A ADBYBY -m set --match-set adbyby_esc dst -j RETURN

	local icount=$(uci show adbyby | grep 'filter_mode' | wc -l)
	let icount=icount-1
	for i in $(seq 0 $icount); do
		local ip=$(uci_get_by_type acl_rule ipaddr '' $i)
		local mode=$(uci_get_by_type acl_rule filter_mode '' $i)
		case "$mode" in
			disable)
				$ipt_n -A ADBYBY -s $ip -j RETURN
				;;
			global)
				$ipt_n -A ADBYBY -s $ip -p tcp -j REDIRECT --to-ports 8118
				$ipt_n -A ADBYBY -s $ip -j RETURN
				;;
		esac
	done

	case $wan_mode in
		0)
			;;
		1)
			ipset -N adbyby_wan hash:ip
			$ipt_n -A ADBYBY -m set ! --match-set adbyby_wan dst -j RETURN
			;;
		2)
			$ipt_n -A ADBYBY -j RETURN
			;;
	esac

	echo "create blockip hash:net family inet hashsize 1024 maxelem 65536" > /tmp/blockip.ipset
	awk '!/^$/&&!/^#/{printf("add blockip %s'" "'\n",$0)}' /usr/share/adbyby/blockip.conf >> /tmp/blockip.ipset
	ipset -! restore < /tmp/blockip.ipset 2>/dev/null
	iptables -I FORWARD -m set --match-set blockip dst -j DROP
	iptables -I OUTPUT -m set --match-set blockip dst -j DROP
}

add_dns()
{
	mkdir -p /tmp/dnsmasq.d/dnsmasq-adbyby.d
	mkdir -p /tmp/dnsmasq.d
	awk '!/^$/&&!/^#/{printf("ipset=/%s/'"adbyby_esc"'\n",$0)}' $PROG_PATH/adesc.conf > /tmp/dnsmasq.d/dnsmasq-adbyby.d/06-dnsmasq.esc
	awk '!/^$/&&!/^#/{printf("address=/%s/'"0.0.0.0"'\n",$0)}' $PROG_PATH/adblack.conf > /tmp/dnsmasq.d/dnsmasq-adbyby.d/07-dnsmasq.black
	echo 'conf-dir=/tmp/dnsmasq.d/dnsmasq-adbyby.d' > /tmp/dnsmasq.d/dnsmasq-adbyby.conf

	local var=1
	if [ $wan_mode -eq 1 ]; then
		awk '!/^$/&&!/^#/{printf("ipset=/%s/'"adbyby_wan"'\n",$0)}' $PROG_PATH/adhost.conf > $WAN_FILE
		if ls /tmp/dnsmasq.d/dnsmasq-adbyby.d/* >/dev/null 2>&1; then
			mkdir -p /tmp/dnsmasq.d
			cp /usr/share/adbyby/dnsmasq.adblock /tmp/dnsmasq.d/dnsmasq-adbyby.d/04-dnsmasq.adblock
			cp /usr/share/adbyby/dnsmasq.ads /tmp/dnsmasq.d/dnsmasq-adbyby.d/05-dnsmasq.ads
		fi
	fi

	mkdir -p /tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/data /tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/host
	rm -f /tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/data/* /tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/host/*
	cp -a /usr/share/adbyby/rules/data/* /tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/data 2>/dev/null
	cp -a /usr/share/adbyby/rules/host/* /tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/host 2>/dev/null
	echo 'addn-hosts=/tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/host/' >> /tmp/dnsmasq.d/dnsmasq-adbyby.conf
	echo 'conf-dir=/tmp/dnsmasq.d/dnsmasq-adbyby.d/rules/data' >> /tmp/dnsmasq.d/dnsmasq-adbyby.conf
	
	[ $block_ios -eq 1 ] && echo 'address=/mesu.apple.com/0.0.0.0' >> /tmp/dnsmasq.d/dnsmasq-adbyby.conf
	if [ $block_cnshort -eq 1 ]; then
		cat <<-EOF >/tmp/etc/dnsmasq-adbyby.d/08-dnsmasq.cnshort
			address=/api.amemv.com/0.0.0.0
			address=/.amemv.com/0.0.0.0
			address=/.tiktokv.com/0.0.0.0
			address=/.snssdk.com/0.0.0.0
			address=/.douyin.com/0.0.0.0
			address=/.ixigua.com/0.0.0.0
			address=/.pstatp.com/0.0.0.0
			address=/.ixiguavideo.com/0.0.0.0
			address=/.v.kandian.qq.com/0.0.0.0
			address=/.yximgs.com/0.0.0.0
			address=/.gifshow.com/0.0.0.0
			address=/.ksapisrv.com/0.0.0.0
			address=/.kuaishoupay.com/0.0.0.0
			address=/.ksyun.com/0.0.0.0
			address=/.live.xycdn.com/0.0.0.0
			address=/.danuoyi.alicdn.com/0.0.0.0
			address=/.v.weishi.qq.com/0.0.0.0
			address=/.pearvideo.com/0.0.0.0
			address=/.miaopai.com/0.0.0.0
			address=/.kuaishou.com/0.0.0.0
			address=/.qupai.me/0.0.0.0
			address=/.meipai.com/0.0.0.0
			address=/.huoshan.com/0.0.0.0
			address=/.ergengtv.com/0.0.0.0
			address=/.baijiahao.baidu.com/0.0.0.0
			address=/.xiongzhang.baidu.com/0.0.0.0
		EOF
	fi
}

del_dns()
{
	rm -f /tmp/dnsmasq.d/dnsmasq-adbyby.conf
	rm -f /tmp/dnsmasq.d/dnsmasq-adbyby.d/*
	rm -f /tmp/adbyby_host.conf
}

add_rule()
{
	$ipt_n -N ADBYBY
	$ipt_n -A ADBYBY -d 0.0.0.0/8 -j RETURN
	$ipt_n -A ADBYBY -d 10.0.0.0/8 -j RETURN
	$ipt_n -A ADBYBY -d 127.0.0.0/8 -j RETURN
	$ipt_n -A ADBYBY -d 169.254.0.0/16 -j RETURN
	$ipt_n -A ADBYBY -d 172.16.0.0/12 -j RETURN
	$ipt_n -A ADBYBY -d 192.168.0.0/16 -j RETURN
	$ipt_n -A ADBYBY -d 224.0.0.0/4 -j RETURN
	$ipt_n -A ADBYBY -d 240.0.0.0/4 -j RETURN
	ip_rule
	if [ $(ipset list music -name -quiet | grep music) ]; then
		$ipt_n -A ADBYBY -m set --match-set music dst -j RETURN 2>/dev/null
	fi
	$ipt_n -A ADBYBY -p tcp -j REDIRECT --to-ports 8118 2>/dev/null
	$ipt_n -I PREROUTING -p tcp --dport 80 -j ADBYBY 2>/dev/null
	mkdir -p /var/etc
	echo -e "/etc/init.d/adbyby restart" > "/var/etc/adbyby.include"
}

del_rule()
{
	$ipt_n -D PREROUTING -p tcp --dport 80 -j ADBYBY 2>/dev/null
	$ipt_n -F ADBYBY 2>/dev/null
	$ipt_n -X ADBYBY 2>/dev/null
	iptables -D FORWARD -m set --match-set blockip dst -j DROP 2>/dev/null
	iptables -D OUTPUT -m set --match-set blockip dst -j DROP 2>/dev/null
	ipset -F adbyby_esc 2>/dev/null
	ipset -X adbyby_esc 2>/dev/null
	ipset -F adbyby_wan 2>/dev/null
	ipset -X adbyby_wan 2>/dev/null
	ipset -F blockip 2>/dev/null
	ipset -X blockip 2>/dev/null
}

reload_rule()
{
	config_load adbyby
	config_foreach get_config adbyby
	del_rule
	[ $enable -eq 0 ] && exit 0
	add_rule
}

start()
{
	config_load adbyby
	config_foreach get_config adbyby

	if [ $enable -ne 0 ]; then
		add_cron
		[ ! -d "/tmp/adbyby/data" ] && cp -a /usr/share/adbyby /tmp/ && rm -f /tmp/adbyby.updated
		add_rules
		/tmp/adbyby/adbyby >/dev/null 2>&1 &
		add_dns
		add_rule
		/etc/init.d/dnsmasq restart
	fi
}

stop()
{
	kill -9 $(busybox ps -w | grep "/tmp/adbyby/adbyby" | grep -v grep | grep -v update| grep -v adblock | awk '{print $1}') >/dev/null 2>&1
	config_load adbyby
	config_foreach get_config adbyby
	del_rule
	del_cron
	del_dns
	/etc/init.d/dnsmasq restart
}

boot()
{
	mkdir -p /tmp/adbyby && cp -a /usr/share/adbyby /tmp/
	start
}

